package com.supreme.securityserver.utils;

import com.supreme.securityserver.constants.EnumRedisKey;
import com.supreme.securityserver.constants.SecurityConst;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

public class TokenUtils {

    public static String createToken(String subject, Map<String, Object> claims, Long expiration) {
        return Jwts.builder()
                .signWith(SignatureAlgorithm.HS512, SecurityConst.JWT_SECRET)
                .setClaims(claims)
                .setIssuer(SecurityConst.JWT_ISSUER)
                .setSubject(subject)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + expiration * 1000))
                .compact();
    }

    public static String createToken(Map<String, Object> claims) {
        return Jwts.builder()
                .setClaims(claims)
                .signWith(SignatureAlgorithm.HS512, SecurityConst.JWT_SECRET).compact();
    }

    public static Claims parseToken(String token) {
        return Jwts.parser()
                .setSigningKey(SecurityConst.JWT_SECRET)
                .parseClaimsJws(token)
                .getBody();
    }

    public static String getTokenKey(String token) {
        return EnumRedisKey.LOGIN_TOKEN.value() + token;
    }
}
